package com.aidong.myprofile.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.myprofile.biz.MyAddressBiz;
import com.aidong.myprofile.entity.MyAddress;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class MyAddressAction implements ModelDriven<MyAddress>{

	private MyAddress myAddress;
	@Override
	public MyAddress getModel() {
		// TODO Auto-generated method stub
		if (myAddress == null) {
			this.myAddress = new MyAddress();
		}
		return myAddress;
	}
	
	private MyAddressBiz myAddressBiz;
	@Autowired
	public void setMyAddressBiz(MyAddressBiz myAddressBiz) {
		this.myAddressBiz = myAddressBiz;
	}
	
	Map<String, Object> map;
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	private int type;
	
	public void setType(int type) {
		this.type = type;
	}

	@Action(value="getreceiveraddress",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getReceiverAddress() {
		map = new HashMap<String, Object>();
		int idongId = myAddress.getIdongId();
		if (idongId >0) {
			try {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId);
				List<Map<String, Object>> addresses = myAddressBiz.getReceiverAddress(paraMap);//所有地址
				map.put("result", addresses);
				map.put("code", 1);
				return "succ";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}

	@Action(value="savereceiveraddress",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addReceiverAddress() {
		map = new HashMap<String, Object>();
		int idongId = myAddress.getIdongId();
		String receiverName = myAddress.getReceiverName();
		String mobilePhone = myAddress.getMobilePhone();
		if (idongId <= 0 || receiverName == null || receiverName.equals("") || mobilePhone == null || mobilePhone.equals("") || type == 0) {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}else {
			String province = myAddress.getProvince();
			String city = myAddress.getCity();
			String country = myAddress.getCountry();
			String detailAddress = myAddress.getDetailAddress();
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("idongId", idongId);
			paraMap.put("receiverName", receiverName);
			paraMap.put("mobilePhone", mobilePhone);
			paraMap.put("province", province);
			paraMap.put("city", city);
			paraMap.put("country", country);
			paraMap.put("detailAddress", detailAddress.replaceAll(" ", ""));
			String mString;
			if (type == 1) {//新增
				myAddressBiz.addReceiverAddress(paraMap);
				mString = "添加成功";
			}else {
				//修改
				int autoId = myAddress.getAutoId();
				if (autoId > 0) {
					paraMap.put("autoId", autoId);
					myAddressBiz.editReceiverAddress(paraMap);
					mString = "修改成功";
				}else {
					map.put("code", 0);
					map.put("msg", "参数缺失");
					return "succ";
				}
			}
			map.put("code", 1);
			map.put("result", mString);
			return "succ";
		}
	}
	
	/**
	 * 删除地址
	 * @return
	 */
	@Action(value="delreceiveraddress",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delReceiverAddress() {
		map = new HashMap<String, Object>();
		int autoId = myAddress.getAutoId();
		if (autoId > 0) {
			try {
				myAddressBiz.delReceiverAddress(autoId);
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
			
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
		
		
	}
	
	/**
	 * 设置默认地址
	 * @return
	 */
	@Action(value="setdefaultreceiveraddress",results={@Result(name="succ",type="json",params={"root","map"})})
	public String setDefaultReceiverAddress() {
		map = new HashMap<String, Object>();
		int autoId = myAddress.getAutoId();
		int idongId = myAddress.getIdongId();
		if (autoId > 0 && idongId >0) {
			try {
				myAddressBiz.setUnDefaultReceiverAddress(idongId);
				myAddressBiz.setDefaultReceiverAddress(autoId);
				map.put("code", 1);
				map.put("result", "设置成功");
				return "succ";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
			
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
}
